package topoMesh;

import hdgeom.primitives.AbstractLine3D;
import hdgeom.primitives.Vec3D;

import java.util.Vector;

import processing.core.PGraphics;
import processing.core.PVector;

public class TopoMeshEdge extends AbstractLine3D {
	TopoMeshFace face1;
	TopoMeshFace face2;
	TopoMeshNode n1;
	TopoMeshNode n2;
	boolean checked;
	Object data;
	int id;

	public TopoMeshEdge(TopoMeshNode n1, TopoMeshNode n2) {
		this.n1 = n1;
		this.n2 = n2;
	}

	public void attach() {
		TopoMeshNode n1 = getNode1();
		n1.getEdges().add(this);
		TopoMeshNode n2 = getNode2();
		n2.getEdges().add(this);
	}

	public void detach() {
		TopoMeshNode n1 = getNode1();
		n1.getEdges().remove(this);
		TopoMeshNode n2 = getNode2();
		n2.getEdges().remove(this);
	}

	public TopoMeshNode getNode1() {
		return (TopoMeshNode) n1;
	}

	public TopoMeshNode getNode2() {
		return (TopoMeshNode) n2;
	}

	public TopoMeshFace getOtherFace(TopoMeshFace f) {
		if (f == face1)
			return face2;
		if (f == face2)
			return face1;
		return null;
	}

	public TopoMeshNode getOtherNode(TopoMeshNode n) {
		if (n1 == n)
			return (TopoMeshNode) n2;
		if (n2 == n)
			return (TopoMeshNode) n1;
		return null;
	}

	public Vec3D getNormal() {
		Vector<Vec3D> normals = new Vector<Vec3D>();
		if (face1 != null) {
			normals.add(face1.getNormal());
		}
		if (face2 != null) {
			normals.add(face2.getNormal());
		}
		if (normals.size() == 0) {
			System.out.println("no Faces on Edge");
		}
		Vec3D f1 = new Vec3D();
		for (int j = 0; j < normals.size(); j++) {
			Vec3D n = normals.get(j);
			f1.add(n);

		}

		f1.normalize();
		if (Float.isNaN(f1.x)) {
			System.out.println("edge has to faces that have sum 0");
			for (int j = 0; j < normals.size(); j++) {
				Vec3D n = normals.get(j);
				System.out.println(n);

			}
		}
		return f1;
	}

	public Vec3D getNormalUnscaled() {
		Vector<Vec3D> normals = new Vector<Vec3D>();
		if (face1 != null) {
			normals.add(face1.getNormalUnscaled());
		}
		if (face2 != null) {
			normals.add(face2.getNormalUnscaled());
		}
		Vec3D f1 = new Vec3D();
		for (int j = 0; j < normals.size(); j++) {
			Vec3D n = normals.get(j);
			f1.add(n);
		}
		return f1;
	}

	public TopoMeshFace getFace(TopoMeshNode n) {
		if (n1 == n)
			return face1;
		if (n2 == n)
			return face2;
		return null;
	}

	public void setFace(TopoMeshNode n, TopoMeshFace face) {
		if (n1 == n) {
			face1 = face;
		} else if (n2 == n) {
			face2 = face;
		} else {
			System.out.println("no node");
		}
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public int getId() {
		// TODO Auto-generated method stub
		return id;
	}

	public void setId(int id) {
		// TODO Auto-generated method stub
		this.id = id;
	}

	public TopoMeshFace getFace1() {
		return face1;
	}

	public void setFace1(TopoMeshFace face1) {
		this.face1 = face1;
	}

	public TopoMeshFace getFace2() {
		return face2;
	}

	public void setFace2(TopoMeshFace face2) {
		this.face2 = face2;
	}

	@Override
	public float x1() {
		// TODO Auto-generated method stub
		return n1.x;
	}

	@Override
	public float y1() {
		// TODO Auto-generated method stub
		return n1.y;
	}

	@Override
	public float z1() {
		// TODO Auto-generated method stub
		return n1.z;
	}

	@Override
	public float x2() {
		// TODO Auto-generated method stub
		return n2.x;
	}

	@Override
	public float y2() {
		// TODO Auto-generated method stub
		return n2.y;
	}

	@Override
	public float z2() {
		// TODO Auto-generated method stub
		return n2.z;
	}

	@Override
	public void setPosition(int i, float x, float y, float z) {
		// TODO Auto-generated method stub
		if (i == 0) {
			n1.x = x;
			n1.y = y;
			n1.z = z;
			return;
		}
		if (i == 1) {
			n2.x = x;
			n2.y = y;
			n2.z = z;
		}
	}

}
